package com.uniview.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.uniview.entity.vo.ResultMessage;
import com.uniview.entity.vo.TableJsonVO;

import net.sf.json.JSONObject;

public class BaseController {
	
	/**
	 * 获取输出的消息
	 * @param message
	 * @param success
	 * @return
	 */
	public ResultMessage getResult(String message,boolean success){
		return new ResultMessage(message,success);
	}
	
	/**
	 * 获取输出的消息
	 * @param message
	 * @param success
	 * @return
	 */
	public ResultMessage getResult(String message,boolean success,Object obj){
		return new ResultMessage(message,success,obj);
	}
	
	
	/**
	 * 获取输出对象
	 * @param message 提示的消息
	 * @param success true 成功 false 失败
	 * @param list  数据对象
	 * @param total 总记录数
	 * @return
	 */
	public ResultMessage getResult(String message,boolean success,List<?> list,Integer total){
		return new ResultMessage(message,success,list,total);
	}
	
	public ResultMessage getResult(String message,boolean success,List<?> list,Object obj){
		return new ResultMessage(message,success,list,obj);
	}
 
	public String getGridJsonString(String data,int total){
		return "{\"data\":"+data+",\"total\":"+total+",\"success\":true}";
	}
	
	public String getTableJsonString(String data,int count){
		return "{\"data\":"+data+",\"code\":\"0\",\"count\":"+count+",\"success\":true}";
	}
	
	public TableJsonVO getTableJsonVO(List<?> data, int count) {
		return new TableJsonVO(data, "0", count, true);
	}
	
	public String getTreeGridJsonString(String data,int total){
		return "{\"rows\":"+data+",\"total\":"+total+",\"success\":true}";
	}
	
	public String getResultJsonString(String msg,boolean result){
		return "{\"success\":"+result+",\"msg\":\""+msg+"\"}";
	}
	
	public String getResultJsonString(String msg,String data,boolean result){
		return "{\"success\":"+result+",\"data\":\""+data+"\",\"msg\":\""+msg+"\"}";
	}
	
	public String getResultJsonString(String msg,JSONObject json,boolean result){
		return "{\"success\":"+result+",\"data\":"+json.toString()+",\"msg\":\""+msg+"\"}";
	}
	
	public String getFormJsonString(String data){
		return "{\"success\":"+true+",\"data\":"+data+"}";
	}
	
	public String getGridJsonForBootstrap(String data,int total){
		return "{\"data\":"+data+",\"recordsTotal\":"+total+",\"recordsFiltered\":"+total+"}";
	}
	
	public String getGridJsonForSelect2(String data,int total){
		return "{\"data\":"+data+",\"total_count\":"+100+",\"incomplete_results\":"+false+"}";
	}
	
	/**
	 * 获取分页信息
	 * @param request
	 * @return
	 */
	public Map<String, Integer> calculatePage (HttpServletRequest request) {
		String start = (String) request.getParameter("start");
		String length = (String) request.getParameter("length");

		// 计算分页页码
		int offset = 0;
		if (StringUtils.isNotBlank(start)) {
			offset = Integer.parseInt(start);
		}
		int pageSize = 10;
		if (StringUtils.isNotBlank(length)) {
			pageSize = Integer.parseInt(length);
		}
		int pageNo = (offset / pageSize) + 1;
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		return map;
	}
	
	protected int getDefaultPageNo(Integer pageNo){
		if(pageNo != null && pageNo >=1) return pageNo;
		return 1;
	}
	
	protected int getDefaultPageSize(Integer pageNo){
		if(pageNo != null && pageNo >=1) return pageNo;
		return 10;
	}
}
